package com.wf.day_0613.test1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * @author mpk
 * @since 2024/6/13 08:59:18
 */
public class Demo {
    // 2.使用LockSupport类的方法 实现 2个生产者 和 2个消费者的模型
    public static void main(String[] args) {

        ArrayList<Integer> list = new ArrayList<>();

        Producer producer = new Producer(list);
        Consumer consumer = new Consumer(list);
        Thread pro1 = new Thread(producer, "生产者1");
        Thread pro2 = new Thread(producer, "生产者2");
        Thread con1 = new Thread(consumer, "消费者1");
        Thread con2 = new Thread(consumer, "消费者2");
        producer.add(con1);
        producer.add(con2);
        consumer.add(pro1);
        consumer.add(pro2);
        pro1.start();
        pro2.start();
        con1.start();
        con2.start();


    }


}

class Consumer implements Runnable {

    private ArrayList<Integer> list;
    private List<Thread> threads = new ArrayList<>();


    public Consumer(ArrayList<Integer> list) {
        this.list = list;
    }

    public void add(Thread thread) {
        threads.add(thread);
    }


    public void run() {
        while (true) {
            synchronized (Consumer.class) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (!list.isEmpty()) {

                    list.remove(0);
                    System.out.println(Thread.currentThread().getName() + " 消费了一个数据" + "还剩" + list.size());

                    LockSupport.unpark(threads.get(0));
                    LockSupport.unpark(threads.get(1));


                } else {
                    System.out.println(Thread.currentThread().getName() + " 没有数据了，等待生产者生产");
                    LockSupport.park();

                }
            }


        }
    }
}

class Producer implements Runnable {
    private ArrayList<Integer> list;

    private List<Thread> threads = new ArrayList<>();

    public Producer(ArrayList<Integer> list) {

        this.list = list;

    }

    public void add(Thread thread) {
        threads.add(thread);
    }

    public void run() {
        while (true) {

            synchronized (Producer.class) {

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (list.size() < 10) {

                    list.add(1);
                    System.out.println(Thread.currentThread().getName() + " 生产了一个数据" + "共有" + list.size());
                    LockSupport.unpark(threads.get(0));
                    LockSupport.unpark(threads.get(1));

                } else if (list.size() == 10) {
                    System.out.println(Thread.currentThread().getName() + "数据满了，等待消费者消费");
                    LockSupport.park();

                }
            }


        }
    }
}